<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of util
 *
 * @author hodek
 */
include 'Constants.php';

class MyTimestamp {

    public $second;
    public $minute;
    public $hour;
    public $day;
    public $month;
    public $year;

}

class Util {

    public static $downloadSection_select_nameById = "SELECT *
FROM `jornak_downloadSection` WHERE `id` = '%s'";
    public static $select_jornakUsers = "SELECT *
FROM `jornak_users` WHERE `login` = '%s' AND `pass` = '%s' ";
    public static $update_jornakUsers = "UPDATE `mikrogen-db`.`jornak_users`
SET `login` = '%s', `pass` = '%s' WHERE `jornak_users`.`id` =1;";
    public static $insert_jornakTechnology = "INSERT INTO `mikrogen-db`.`jornak_technology` (
`id` ,
`headline` ,
`text` ,
`list` ,
`imgs`,
`sectionId`
)
VALUES (
NULL , '%s', '%s', '%s', '%s', '%s'
);";
    public static $technologySection_selectAll = "SELECT *
FROM `jornak_technologySection`";
    public static $select_jornakTechnologyBySectionId = "SELECT *
FROM `jornak_technology` WHERE sectionId = '%s' ORDER BY id DESC";
    public static $insert_jornakImages = "INSERT INTO `mikrogen-db`.`jornak_images` (
`id` ,
`full` ,
`ext` ,
`width` ,
`height`
)
VALUES (
NULL , '%s', '%s', '%s', '%s'
);";
    public static $select_jornakImages = "SELECT *
FROM `jornak_images`
WHERE `id` ='%s'";
    public static $insert_jornakText = "INSERT INTO `mikrogen-db`.`jornak_text` (
`id` ,
`header` ,
`text` ,
`list` ,
`video` ,
`imgs` ,
`present`
)
VALUES (
NULL , '%s', '%s', '%s', '%s' , '%s' , '%s'
);";
    public static $delete_jornakDestination = "DELETE FROM `mikrogen-db`.`jornak_destination` WHERE `jornak_destination`.`id` = '%s'";
    public static $delete_jornakDestinationByTextId = "DELETE FROM `mikrogen-db`.`jornak_destination` WHERE `jornak_destination`.`textid` = '%s'";
    public static $insert_jornakDestination = "INSERT INTO `mikrogen-db`.`jornak_destination` (
`id` ,
`typeid` ,
`textid` ,
`time` ,
`preview`
)
VALUES (
NULL , '%s', '%s', '%s' , '0'
);";
    public static $edit_jornakDestinationChangeToNotPreview = "UPDATE `mikrogen-db`.`jornak_destination`
SET `preview` = '0' WHERE `jornak_destination`.`id` = '%s';";
    public static $insert_jornakDestinationPreview = "INSERT INTO `mikrogen-db`.`jornak_destination` (
`id` ,
`typeid` ,
`textid` ,
`time` ,
`preview`
)
VALUES (
NULL , '%s', '%s', '%s' , '1'
);";
    public static $select_jornakDestination = "SELECT *
FROM `jornak_destination`
WHERE `typeid` = '%s' AND `preview` = 0 ORDER BY time DESC";
    public static $select_jornakDestinationAll = "SELECT *
FROM `jornak_destination`
WHERE `typeid` = %s ORDER BY time DESC";
    public static $select_jornakDestinationAllById = "SELECT *
FROM `jornak_destination`
WHERE `id` = %s";
    public static $select_validNews = "SELECT b.header, b.present, a.time 
        FROM jornak_destination a LEFT JOIN jornak_text b ON (a.textid = b.id) 
        WHERE a.typeid = 1 AND a.preview = 0 AND b.present != ''";
    public static $select_jornakDestinationByTextId = "SELECT *
FROM `jornak_destination`
WHERE `textid` = %s AND `preview` = '0' ORDER BY time DESC";
    public static $select_jornakDestinationAllByTextId = "SELECT *
FROM `jornak_destination`
WHERE `textid` = %s ORDER BY time DESC";
    public static $select_jornakText = "SELECT *
FROM `jornak_text`
WHERE `id` = '%s' ";

    public static function parseDateTime($dateTime) {
        $myArray = explode(" ", $dateTime);
        $myArray = explode("-", $myArray[0]);
        $date = Constants::$month[(int) $myArray[1] - 1] . ' ' . (int) $myArray[2] . ', ' . $myArray[0];
        return $date;
    }

    public static function getYearFromDBDateTime($dateTime) {
        $myArray = explode("-", $dateTime);
        return $myArray[0];
    }

    public static function getMonthFromDBDateTime($dateTime) {
        $myArray = explode("-", $dateTime);
        return (int) $myArray[1];
    }

    public static function getDayFromDBDateTime($dateTime) {
        //$myArray = explode(" ",$dateTime);
        $myArray = explode("-", $dateTime);
        return (int) $myArray[2];
    }

    /**
     *
     * @param type $dateTime
     * @return MyTimestamp 
     */
    public static function getTimestampFromDBDateTime($dateTime) {
        $secondArray = explode(" ", $dateTime);
        $firstArray = explode("-", $secondArray[0]);
        $secondArray = explode(":", $secondArray[1]);
        $time = new MyTimestamp();
        $time->year = $firstArray[0];
        $time->month = $firstArray[1];
        $time->day = $firstArray[2];
        $time->hour = $secondArray[0];
        $time->minute = $secondArray[1];
        $time->second = $secondArray[2];
        return $time;
    }

    public static function getLink() {
        //echo $dbUserName;
        $link = mysql_connect(Constants::$dbServer, Constants::$dbUserName, Constants::$dbPass);
        if (!$link) {
            die('Could not connect: ' . mysql_error());
        }
        //echo 'Connected successfully <br>';
        $db_selected = mysql_select_db(Constants::$db, $link);
        if (!$db_selected) {
            die('Can\'t use ' . Constants::$db . ' : ' . mysql_error());
        }
        //mysql_close($link);
        return $link;
    }

    public static function closeLink($link) {
        mysql_close($link);
    }

    public static function isNull($check) {
        if ($check == null)
            return true;
        return false;
    }

    public static function isNotNull($check) {
        if ($check == null)
            return false;
        return true;
    }

    public static function hash($user, $pass) {
        return md5($user . $pass . matujetlustouckej);
    }

    public static function getCurrentDateTime() {
        return date('Y-m-d h:i:s');
    }

    public static function getCurrentRSSTime() {
        return date(DATE_RFC2822);
    }

    public static function sendMail($subject, $body) {
        $to = "info@fibix.org";
        return mail($to, $subject, $body);
    }

    public static function sendMailTo($subject, $body, $to) {
        return mail($to, $subject, $body);
    }

    public static function sendMailToFull($to, $subject, $body, $headers) {
        return mail($to, $subject, $body, $headers);
    }

    public static function getUploadedFiles() {
        // open this directory
        $myDirectory = opendir("../" . Constants::$uploads_dir);
        // $myDirectory = opendir(".");
        // get each entry
        while ($entryName = readdir($myDirectory)) {
            if ($entryName != "." && $entryName != "..") {
                $dirArray[] = $entryName;
            }
        }

        // close directory
        closedir($myDirectory);


        // sort 'em
        sort($dirArray);

        return $dirArray;
    }

    public static function unescape($string) {
        return str_replace("\'", "'", $string);
    }

}

?>
